package com.server.service;

import com.server.Untils.MybatisClass;
import com.server.Untils.ResponseMessage;
import com.server.entity.Items;
import com.server.example.ItemsExample;
import com.server.mapper.ItemsMapper;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Admin
 */
public class ItemsService {

    /**
     * 加入购物车功能实现
     * <br/>
     * 调用方法：String itemsInfo = new ItemsService().insertItems(items);
     * - 成功 The request is successful
     * - 失败 Other unknown errors,Please contact the developer
     * <br/>
     * @param item 传递的对象
     * @return 返回是否成功
     */
    public String insertItems(Items item) {
        String returnString="";
        SqlSession sqlSession = MybatisClass.getSqlSession();
        ItemsMapper itemMapper = sqlSession.getMapper(ItemsMapper.class);
        try {
            int types=itemMapper.insertSelective(item);
            returnString=types==1? ResponseMessage.LOGIN_SUCCESS:ResponseMessage.ANOTHER_ERROR;
        }catch (Exception e) {
            returnString=ResponseMessage.SERVER_ERROR;
        }finally {
            sqlSession.commit();
            sqlSession.close();
        }
        return returnString;
    }

    /**
     * 购买图书
     * <br/>
     * 调用方法：String itemsInfo = new ItemsService().updateItems(items);
     * - 成功 The request is successful
     * - 失败 Other unknown errors,Please contact the developer
     * <br/>
     * @param item 传递的对象
     * @return 返回是否成功
     */
    public String updateItems(Items item) {
        String returnString="";
        SqlSession sqlSession = MybatisClass.getSqlSession();
        ItemsMapper itemMapper = sqlSession.getMapper(ItemsMapper.class);
        try {
            int types=itemMapper.updateByPrimaryKeySelective(item);
            returnString=types>=1? ResponseMessage.LOGIN_SUCCESS:ResponseMessage.ANOTHER_ERROR;
        }catch (Exception e) {
            returnString=ResponseMessage.SERVER_ERROR;
        }finally {
            sqlSession.commit();
            sqlSession.close();
        }
        return returnString;
    }

    /**
     * 删除数据，及移除订单信息
     * @param item 订单的id编号
     * @return 返回是否成功
     */
    public String deleteItem(int item){
        String returnString="";
        SqlSession sqlSession = MybatisClass.getSqlSession();
        ItemsMapper itemMapper = sqlSession.getMapper(ItemsMapper.class);
        try {
            int types=itemMapper.deleteByPrimaryKey(item);
            returnString=types>=1? ResponseMessage.LOGIN_SUCCESS:ResponseMessage.ANOTHER_ERROR;
        }catch (Exception e) {
            returnString=ResponseMessage.SERVER_ERROR;
        }finally {
            sqlSession.commit();
            sqlSession.close();
        }
        return returnString;
    }

    /**
     * 查询数据:通过用户的id去进行查询，返回查询集
     * @param userList 查询的用户id集合
     * @return 返回集合
     */
    public List<Items> itemsList(List<Integer>userList){
        List<Items> litems=null;
        SqlSession sqlSession = MybatisClass.getSqlSession();
        ItemsMapper itemMapper = sqlSession.getMapper(ItemsMapper.class);
        try {
            ItemsExample example = new ItemsExample();
            if (userList.size()>0) {
                example.createCriteria().andIidIsNotNull().andOidIn(userList);
            }else {
                example.createCriteria().andIidIsNotNull();
            }
            litems=itemMapper.selectByExample(example);
        }catch (Exception e) {
            litems = new ArrayList<>();
        }finally {
            sqlSession.commit();
            sqlSession.close();
        }
        return litems;
    }
}
